<template>
  <div class="edu-page-warp">
    <ConditionCard body-style="padding: 24px 24px 6px;" @search="search" @reset="reset">
      <el-form :model="condition" label-width="100px" ref="conditionForm">
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item :label="$t('auditBatch.compeleteGraduationYear') + ':'" prop="graduationYear">
              <el-date-picker
                v-model="condition.graduationYear"
                value-format="yyyy"
                align="right"
                type="year"
                :placeholder="$t('common.pla.pleaseSelect')"
                clearable
                style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('auditBatch.auditBatch') + ':'" prop="auditBatch">
              <el-select v-model="condition.auditBatch" style="width: 100%" filterable clearable>
                <el-option v-for="item in options.auditBatch" :key="item.key" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('auditBatch.useType') + ':'" prop="useType">
              <el-select v-model="condition.useType" style="width: 100%" filterable clearable>
                <el-option v-for="item in options.useType" :key="item.id" :label="item.value" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </ConditionCard>
    <el-card shadow="never" body-style="padding: 24px;">
      <div class="clearfix" style="margin-bottom:16px">
        <div class="edu-page-title float-left audit-title">{{$t("auditBatch.completeBatchList")}}</div>
        <div class="float-right">
          <el-button type="danger" @click="handleLeaveShool">{{$t("completeAuditBatch.permissionForGraduates")}}</el-button>
          <el-button type="primary" @click="addBatch">{{$t("auditBatch.add")}}</el-button>
        </div>
      </div>
      <edu-table :data="tableData" :order="false" @selection-change="checkBoxChange" :pageNum="page.pageNum"
        :pageSize="page.pageSize" :total="page.total" @page-change="pageChange" ref="auditBatchTable">
        <edu-table-column :label="$t('auditBatch.compeleteGraduationYear')" width="80" align="center" prop="graduateData">
        </edu-table-column>
        <edu-table-column :label="$t('auditBatch.auditBatch')" width="80" align="center" prop="reviewBatchI18n">
        </edu-table-column>
        <edu-table-column :label="$t('graduationRuth.auditTimeArrage')" align="center" width="260">
          <template slot-scope="scope">
            <div>{{scope.row.collegeReviewStartDate}}-{{scope.row.collegeReviewEndDate}}</div>
          </template>
        </edu-table-column>
        <edu-table-column :label="$t('auditBatch.graduationCertificateDate')" min-width="110" align="center"
          prop="graduationCertificateDate">
        </edu-table-column>
        <edu-table-column :label="$t('auditBatch.leaveSchoolDate')" min-width="160" align="center"
          prop="leaveSchoolDate">
        </edu-table-column>
        <edu-table-column :label="$t('auditBatch.permissionForGraduatesStatus')" align="center" width="160">
          <template slot-scope="scope">
            <div v-if="scope.row.leaveOperStatus == 0 ">
              <el-tag size="mini" type="success">
              {{$t('common.text.yes')}}</el-tag>
            </div>
            <div v-else>
              <el-tag size="mini" type="danger">
                {{$t('common.text.no')}}</el-tag>
            </div>
          </template>
        </edu-table-column>
        <edu-table-column :label="$t('auditBatch.useType')" width="80" align="center">
          <template slot-scope="scope">
            <div v-if="useTypeMap[scope.row.status]">
              <el-tag size="mini" :type="useTypeMap[scope.row.status].type">
                {{useTypeMap[scope.row.status].label}}</el-tag>
            </div>
          </template>
        </edu-table-column>
        <edu-table-column :label="$t('auditBatch.degreeResolutionNo')" min-width="120" align="center"
          prop="degreeResolutionNo">
        </edu-table-column>
        <edu-table-column :label="$t('common.text.operation')" align="center" min-width="50" fixed='right'>
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click.stop="editBatch(scope.row)">{{$t('auditBatch.edit')}}</el-button>
          </template>
        </edu-table-column>
      </edu-table>
    </el-card>
    <div v-if="addBacthWinShow">
      <add-batch-win :show="addBacthWinShow" @updateShow="updateShow" :title="addWinTitle" :editData="editData">
      </add-batch-win>
    </div>
  </div>
</template>

<script>
  import {
    queryDic
  } from "common/src/api/dictionary";
  import UTILS from "common/src/utils/utils";
  import {completeGetGraduateReviewBatch, completeManualSetLeaveSchool} from 'common/src/api/graduationManagement/graduationManage';
  import {
    forMateTimeToStr,
    changeTimeToYmdhm
  } from '../components/gradMangeUtis';
  export default {
    name: 'auditBatch',
    data() {
      let currentYear = new Date().getFullYear();
      return {
        condition: {
          graduationYear: currentYear + '',
          auditBatch: '',
          useType: ''
        },
        options: {
          auditBatch: [],
          useType: [{
            id: 1,
            value: this.$t('graduationRuth.opened'),
          }, {
            id: 0,
            value: this.$t('graduationRuth.closed'),
          }]
        },
        useTypeMap: {
          0: {
            label: this.$t('graduationRuth.closed'),
            type: 'danger'
          },
          1: {
            label: this.$t('graduationRuth.opened'),
            type: 'success'
          }
        },
        tableData: [],
        page: {
          total: 0,
          pageNum: 1,
          pageSize: 20
        },
        isOverseas: '0',
        addBacthWinShow: false,
        addWinTitle: '',
        editData: ''
      }
    },
    created() {
      this.initAuditBatchOptions();
      this.query();
    },
    methods: {
      initAuditBatchOptions() {
        const data = {
          lang: this.$store.getters.language || "cn",
          type: "allChild",
          keys: ["X_SHPC"]
        };
        queryDic(data).then(res => {
          if (res.code === 200) {
            UTILS.fillSelect({
                auditBatch: "X_SHPC"
              },
              res.data,
              this.options
            );
          }
        });
      },
      addBatch() {
        this.editData = '';
        this.addWinTitle = this.$t('common.btn.add');
        this.addBacthWinShow = true;
      },
      reset() {
        this.$refs.conditionForm.resetFields();
        this.page.pageNum = 1;
        this.query();
      },
      search() {
        this.page.pageNum = 1;
        this.query();
      },
      query({
        pageSize,
        pageNum
      } = {}) {
        let param = {
          isOverseas: this.isOverseas,
          pageNum_: pageNum || this.page.pageNum,
          pageSize_: pageSize || this.page.pageSize
        };
        if (this.condition.graduationYear) {
          param.graduateData = this.condition.graduationYear;
        }
        if (this.condition.auditBatch) {
          param.reviewBatch = this.condition.auditBatch;
        }
        if (this.condition.useType !== '') {
          param.status = this.condition.useType;
        }
        this.checkList = null;
        completeGetGraduateReviewBatch(param).then(res => {
          if (res && res.code === 200) {
            let data = res.data || {};
            let list = data.list || [];
            this.updateList(list);
            this.tableData = list;
            this.page.total = data.total_;
            this.page.pageNum = data.pageNum_;
            this.page.pageSize = data.pageSize_;
          }
        }, () => {
          this.tableData = [];
          this.page.total = 0;
          this.page.pageNum = 1;
          this.page.pageSize = param.pageSize_;
        })
      },
      updateList(data){
        data.forEach(item =>{
          if (item.graduationCertificateDate){
            item.graduationCertificateDate = item.graduationCertificateDate.split(" ")[0];
          }
          if (item.collegeReviewStartDate) {
            item.collegeReviewStartDate = changeTimeToYmdhm(item.collegeReviewStartDate);
          }
          if (item.collegeReviewEndDate) {
            item.collegeReviewEndDate = changeTimeToYmdhm(item.collegeReviewEndDate)
          }
          if (item.leaveSchoolDate) {
            item.leaveSchoolDate = changeTimeToYmdhm(item.leaveSchoolDate)
          }
        })
      },
      handleLeaveShool() {
        let checkData = this.$refs.auditBatchTable.getCheckData(); 
        if (!this.checkList || !this.checkList.length) {
          this.$message({
            message: this.$t('graduationRuth.selectTip'),
            type: 'warning'
          });
          return;
        }else {
          let currentTime = new Date().getTime();
          let isCanLeaveSchool = true;
          let isHasLeaveSchool = false;
          checkData.forEach(item => {
            let leaveTime = forMateTimeToStr(item.leaveSchoolDate);
            if (leaveTime > currentTime) {
              isCanLeaveSchool = false;
            }
            if (item.leaveOperStatus == 0) {
              isHasLeaveSchool = true;
            }
          })
          if (!isCanLeaveSchool) {
            this.$message({
              message: this.$t('auditBatchValidate.validLeaveSchoolDateTip'),
              type: 'warning'
            });
            return;
          };
          if (isHasLeaveSchool) {
            this.$eduConfirm(this.$t('auditBatch.permissionForGraduatesStatusTip'), this.$t('completeAuditBatch.permissionForGraduates')).then(() => {
              this.leaveSchoolFn();
            })
          }else {
            this.leaveSchoolFn();
          }
        }
      },
      leaveSchoolFn() {
         completeManualSetLeaveSchool(this.checkList).then(res =>{
            if (res.code === 200) {
              this.$message({
                message: this.$t('common.tips.opSuccess'),
                type: 'success'
              });
              this.query();
            }
          })
      },
      checkBoxChange(checkArr) {
        let ids = [];
        checkArr.forEach(item => {
          ids.push(item.id);
        });
        this.checkList = ids;
      },
      pageChange(pageOption) {
        this.query(pageOption);
      },
      editBatch(row) {
        this.editData = row;
        this.addWinTitle = this.$t('common.btn.edit');
        this.addBacthWinShow = true;
      },
      updateShow() {
        this.addBacthWinShow = false;
      }
    },
    components: {
      addBatchWin: resolve => require(['./addAuditBatchWin.vue'], resolve)
    }
  }

</script>

<style lang="scss" scoped>
  .audit-title {
    line-height: 32px;
    margin-bottom: 0;
  }

</style>
